package game.users.service.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import game.level.pojo.ALevel;
import game.level.service.IALevelService;
import game.level.service.impl.ALevelServiceImpl;
import game.users.dao.IAUsersDao;
import game.users.dao.impl.AUsersDaoImpl;
import game.users.pojo.AUsers;
import game.users.service.IAUsersService;

public class AUsersServiceImpl implements IAUsersService
{
	private IAUsersDao ausers = new AUsersDaoImpl();
	/*添加一条记录*/
	public int insertService(AUsers users)
	{
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("email", users.getEmail());
		AUsers usersi = this.findOndAUsersService(condMap);
		if(usersi !=null)
		{
			/*此邮箱已存在 */
			System.out.println("==您输入的邮箱已经存在==邮箱:" + users.getEmail() 
				+ ";重复的人:" + usersi.getId() + ";昵称:" + usersi.getNickName());
			return 0 ; 
		}
		return this.ausers.insertDao(users);
	}
	/*跟新一条记录*/
	public int updateService(AUsers users)
	{
		return this.ausers.updateDao(users);
	}
	/*删除一条记录*/
	public int deleteService(AUsers users)
	{
		return this.ausers.deleteDao(users);
	}
	/*查询一条记录*/
	public AUsers findOndAUsersService(Map<String, Object> condMap)
	{
		return this.ausers.findOndDao(condMap);
	}
	/*查询所有记录*/
	private IALevelService levelService = new ALevelServiceImpl();
	public List<AUsers> findCondListService(Map<String, Object> condMap)
	{
		List<AUsers> usersList = this.ausers.findCondListDao(condMap);
		Map<String, Object> extMap = new HashMap<String, Object>();
		for (Iterator iterator = usersList.iterator(); iterator.hasNext();)
		{
			AUsers users = (AUsers) iterator.next();
			/*根据用户表查询 等级表的id*/
			extMap.clear();
			extMap.put("id", users.getLevelId());
			ALevel level = this.levelService.findOneLevelService(extMap);
			/*将结果给用户表*/
			users.setLevel(level);
		}
		return usersList;
	}

}
